package no.ntnu.fp.comparators;

import java.util.Comparator;

public class AvtaleComparator implements Comparator<String> {

	@Override
	public int compare(String o1, String o2) {
		String year1 = o1.substring(10, 12);
		String year2 = o2.substring(10, 12);
		int year1int = Integer.parseInt(year1);
		int year2int = Integer.parseInt(year2);
		
		System.out.println("ar 1: " + year1int + " ar 2: " + year2int);
		
		
		if (year1int != year2int)
			return compareTo(year1int, year2int);
		
		int month1 = Integer.parseInt(o1.substring(8, 10));
		int month2 = Integer.parseInt(o2.substring(8, 10));
		
		if (month1 != month2)
			return compareTo(month1, month2);
		
		int day1 = Integer.parseInt(o1.substring(6, 8));
		int day2 = Integer.parseInt(o2.substring(6, 8));
		
		if (day1 != day2)
			return compareTo(day1, day2);
		
		int startHour1 = Integer.parseInt(o1.substring(31, 33));
		int startMinute1 = Integer.parseInt(o1.substring(34, 36));
		
		int startHour2 = Integer.parseInt(o2.substring(31, 33));
		int startMinute2 = Integer.parseInt(o2.substring(34, 36));
		
		if (startHour1 != startHour2)
			return compareTo(startHour1, startHour2);
		
		if (startMinute1 != startMinute2)
			return compareTo(startMinute1, startMinute2);
		
		
		int stopHour1 = Integer.parseInt(o1.substring(54, 56));
		int stopMinute1 = Integer.parseInt(o1.substring(57, 59));
		
		int stopHour2 = Integer.parseInt(o2.substring(54, 56));
		int stopMinute2 = Integer.parseInt(o2.substring(57, 59));
		
		if (stopHour1 != stopHour2)
			return compareTo(stopHour1, stopHour2);
		
		if (stopMinute1 != stopMinute2)
			return compareTo(stopMinute1, stopMinute2);

		return 0;
	}
	
	private int compareTo(int one, int two) {
		int firstBigger = 1, firstLesser = -1;
		return (one > two) ? firstBigger : firstLesser;
	}
}
